package server;

import java.util.HashMap;
import java.util.Map;

/* Routes paths to servlets */
public class Router {
	private Map<String, Servable> routes;
	
	
	public Router() {
		routes = new HashMap<String, Servable>();		
	}
	
	public void addServlet(String route, Servable servlet) {
		routes.put(route, servlet);
	}
	
	public Servable getServlet(Request request) {
		String query = request.header.getUrlQuery();	
		return routes.get(query);
	}	
	
	public boolean serve(Request request, Response response) {		
		String route = request.header.getUrl();		
		Servable servlet = this.getServlet(request);
		if (servlet != null) {
			response.header.setContentType(servlet.getContentType());
			
			servlet.serve(request, response);
			response.header.setStatus(200);
			return true;
		}		
		return false;
	}
	
}
